﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DAO_ADMIN;
using System.Diagnostics;
using System.Data.SqlClient;
using DTO;
namespace Demo_CSDL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            progressBar1.Maximum = progressBar1.Width;
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           }

        private void dgvShowData_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow currentrow = dgvShowData.CurrentRow;
            if (currentrow == null)
            {
                return;
            }
            int index = dgvShowData.CurrentRow.Index;
            if (dgvShowData.CurrentRow != null)
            {
                try
                {
                    //lay thong tin chi tiet nguyen lieu
                    int _MaBN = (int)dgvShowData.Rows[index].Cells[0].Value;
                    string _CMND = dgvShowData.Rows[index].Cells[1].Value.ToString();
                    string _DienThoai = dgvShowData.Rows[index].Cells[2].Value.ToString();
                    string _HoTen = dgvShowData.Rows[index].Cells[3].Value.ToString();
                    string _NgaySinh = dgvShowData.Rows[index].Cells[4].Value.ToString();
                    string _DiaChi = dgvShowData.Rows[index].Cells[5].Value.ToString();
                    txtMaBNTK.Text = _MaBN.ToString();
                    txtCMNDTK.Text = _CMND;
                    txtSDTTK.Text = _DienThoai;
                    txtHoTenTK.Text = _HoTen;
                    txtNgaySinhTK.Text = _NgaySinh;
                    txtDiaChiTK.Text = _DiaChi;
                }
                catch { }
            }
        }

        private void btnThemHSBN_Click(object sender, EventArgs e)
        {
            string _CMND = txtCMNDHSBN.Text;
            string _DienThoai = txtSDTHSBN.Text;
            string _NgaySinh = dtpNgaySinhHSBN.Text;
            string _DiaChi = txtDiaChiHSBN.Text ;
            string _HoTen = txtHoTenHSBN.Text;
            string _GioiTinh = cbxGioiTinhHSBN.Text;
            string _SoTheBH = txtSoTheBHHSBN.Text;
            string _NgayCap = dtpNgayCapHSBN.Text;
            string _NgayHetHan = dtpNgayHetHanHSBN.Text;

            BenhNhanDTO benhnhan = new BenhNhanDTO();

            benhnhan.CMND = _CMND;
            benhnhan.DienThoai = _DienThoai;
            benhnhan.NgaySinh = DateTime.Parse(_NgaySinh);
            benhnhan.DiaChi = _DiaChi;
            benhnhan.HoTen = _HoTen;
            benhnhan.GioiTinh = cbxGioiTinhHSBN.SelectedIndex;
            benhnhan.SoThe = int.Parse(_SoTheBH);
            benhnhan.NgayHetHan = DateTime.Parse(_NgayHetHan);
            Stopwatch /*sw1,*/sw2;
            //sw1= new Stopwatch();
            sw2 = new Stopwatch();
            //sw1.Start();
            DataTable dt = BenhNhanBUS.LayDanhSachBenhNhan_CMND(benhnhan.CMND);
            //sw1.Stop();
            //lblGiayCMNDHSBN.Text = ((float)sw1.ElapsedMilliseconds / 1000).ToString() + " giây";
            if (dt.Rows.Count > 0)
            {
                DialogResult result1 = MessageBox.Show("Bênh nhân này đã tồn tại! Bản có muốn tiếp tục không?", "Thông báo", MessageBoxButtons.YesNo);
                if (result1 == DialogResult.Yes)
                {
                    sw2.Start();
                    bool isthemthanhcong = BenhNhanBUS.ThemBanhNhanMoi(benhnhan);
                    sw2.Stop();
                    lblAllTimeThemHSBN.Text = ((float)sw2.ElapsedMilliseconds / 1000).ToString() + " giây";

                    if (isthemthanhcong) MessageBox.Show("Thêm thành công");
                    else MessageBox.Show("Thêm thất bại");
                }
            }
            else
            {
                sw2.Start();
                bool isthemthanhcong = BenhNhanBUS.ThemBanhNhanMoi(benhnhan);
                sw2.Stop();
                lblAllTimeThemHSBN.Text = ((float)sw2.ElapsedMilliseconds / 1000).ToString() + " giây";

                if (isthemthanhcong) MessageBox.Show("Thêm thành công");
                else MessageBox.Show("Thêm thất bại"); 
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                string _input = txtTimKiem.Text;
                DataTable dt = BenhNhanBUS.LayDanhSachBenhNhan(_input);
                dgvShowData.DataSource = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            sw.Stop();
            lblGiay.Text = ((float)sw.ElapsedMilliseconds / 1000).ToString() + " giây";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            
            Stopwatch sw = new Stopwatch();
            sw.Start();
            int iMaHoSo = int.Parse(txtMaHSXuatVien.Text);
            XuatVienDTO ThongTinXuatVien = new XuatVienDTO();
            XuatVienBUS xvBUS = new XuatVienBUS();
            try
            {
                ThongTinXuatVien = xvBUS.LayThongTinXuatVien(iMaHoSo);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            sw.Stop();
            lblTenBenhNhan.Text = ThongTinXuatVien.TENBENHNHAN;
            lblHoaDonDichVu.Text = ThongTinXuatVien.TONGTIENDICHVU.ToString("0,0" + " VNĐ");
            lblHoaDonPhong.Text = String.Format("{0:0,0 VNĐ}", ThongTinXuatVien.TONGTIENPHONG);
            lblHoaDonThuoc.Text = ThongTinXuatVien.TONGTIENTHUOC.ToString("0,0" + " VNĐ");
            float tongtien = ThongTinXuatVien.TONGTIENPHONG + ThongTinXuatVien.TONGTIENTHUOC + ThongTinXuatVien.TONGTIENDICHVU;
            lblTongTien.Text =  String.Format("{0:0,0 VNĐ}", tongtien);
            lblTimeSearch.Text = ((float)sw.ElapsedMilliseconds / 1000).ToString() + " giây";
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            XuatVienBUS xuatvienBUS = new XuatVienBUS();
            int iMaHS;
            try
            {
                iMaHS = int.Parse((txtMaHSXuatVien.Text));
            }
            catch
            {
                MessageBox.Show("Sai định dạng");
                return;
            }
            bool bKQ = false;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                bKQ = xuatvienBUS.ThanhToanHoaDonXuatVien(iMaHS);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            sw.Stop();
            lblThanhToan.Text = ((float)sw.ElapsedMilliseconds / 1000).ToString() + " giây";

            if (bKQ == true)
            {
                MessageBox.Show("Thanh toán hóa đơn xuất viện thành công");
            }
        }
    }
}
